package com.example.springbootredis.repository;

import com.example.springbootredis.entiy.ShippingAddress;
import org.springframework.data.jdbc.repository.query.Modifying;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface ShippingAddressRepository extends JpaRepository<ShippingAddress, Long> {
    List<ShippingAddress> findByMemberIdOrderByIsDescCreateTimeDesc(Long memberId);
    Optional<ShippingAddress> findByIdAndMemberId(Long id, Long memberId);
    Optional<ShippingAddress> findByIdAndMemberIdAndIsDefault( Long memberId, Boolean isDefault);

    @Modifying
    @Query("UPDTAE ShippingAddress sa SET sa.isDefault +false WHERE sa.memberId = :memberId AND sa.isDefault = true")
    void clearDefaultShippingAddressByMemberId(Long memberId);

    boolean existsByMemberIdAndIsDefault(Long id, Boolean isDefault);

    List<ShippingAddress> findByMemberIdOrderByCreatedDateDesc(Long memberId);


    void clearDefaultAddress(Long memberId);
}
